package com.example.cms;
import com.example.cms.Appointments.AppointmentsManager;
import com.example.cms.Departments.DepartmentsManager;
import com.example.cms.Doctors.DoctorsManager;
import com.example.cms.Patients.PatientsManager;
import com.example.cms.Payments.PaymentManager;
import com.example.cms.Queues.QueuesManager;
import com.example.cms.Schedules.SchedulesManager;
import com.example.cms.Users.User;
import com.example.cms.Users.UserService;
import com.example.cms.AuthUtils.*;
import com.example.cms.Users.UsersManager;
import java.sql.*;
import java.util.*;

public class CMSManager {
    private Scanner scanner;
    private User currentUser;
    private UserService userService;
    private DepartmentsManager departmentsManager;
    private DoctorsManager doctorsManager;
    private PatientsManager patientsManager;
    private SchedulesManager schedulesManager;
    private AppointmentsManager appointmentsManager;
    private QueuesManager queuesManager;
    private UsersManager usersManager;
    private PaymentManager paymentManager;
   //  初始化各个管理器
    public CMSManager() {
        this.scanner = new Scanner(System.in);
        this.userService = new UserService();
        this.patientsManager = new PatientsManager();
        this.doctorsManager = new DoctorsManager();
        this.paymentManager = new PaymentManager();
        this.queuesManager = new QueuesManager();
        this.schedulesManager = new SchedulesManager();
        this.departmentsManager = new DepartmentsManager();
        this.usersManager = new UsersManager();
        this.appointmentsManager = new AppointmentsManager();
        // 设置依赖关系
        setupDependencies();
    }

    private void setupDependencies() {
        // 设置 AppointmentsManager 的依赖
        appointmentsManager.setSchedulesManager(schedulesManager);
        appointmentsManager.setPatientsManager(patientsManager);
        appointmentsManager.setDoctorsManager(doctorsManager);
        appointmentsManager.setUsersManager(usersManager);
        // 设置 DoctorsManager 的依赖
        doctorsManager.setSchedulesManager(schedulesManager);
        doctorsManager.setUserService(userService);
        // 设置 DepartmentsManager 的依赖
        departmentsManager.setDoctorsManager(doctorsManager);
        // 设置 PaymentManager 的依赖
        paymentManager.setAppointmentsManager(appointmentsManager);
        // 设置 QueuesManager 的依赖
        queuesManager.setAppointmentsManager(appointmentsManager);
        queuesManager.setSchedulesManager(schedulesManager);
        // 设置 UsersManager 的依赖
        usersManager.setSchedulesManager(schedulesManager);
        usersManager.setCurrentUser(currentUser);
    }

    public void start() {
        System.out.println("=== 医院门诊管理系统 ===");
        login();

        if (currentUser != null) {
            // 登录成功后设置所有管理器的当前用户
            setCurrentUserAll(currentUser);
            showMainMenu();
        } else {
            System.out.println("登录失败，系统退出！");
        }

        scanner.close();
    }

    private void setCurrentUserAll(User currentUser) {
        // 设置各个管理器的当前用户
        usersManager.setCurrentUser(currentUser);
        appointmentsManager.setCurrentUser(currentUser);
        schedulesManager.setCurrentUser(currentUser);
        patientsManager.setCurrentUser(currentUser);
        doctorsManager.setCurrentUser(currentUser);
        queuesManager.setCurrentUser(currentUser);
        paymentManager.setCurrentUser(currentUser);
    }


    private void login() {
        System.out.println("\n--- 用户登录 ---");
        System.out.print("用户名: ");
        String username = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();

        currentUser = userService.login(username, password);

        if (currentUser != null) {
            System.out.println("登录成功！欢迎 " + currentUser.getName() +
                    " [" + AuthUtil.getRoleDisplayName(currentUser.getRole()) + "]");
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    private void showMainMenu() {
        while (true) {
            System.out.println("\n=== 主菜单 ===");
            System.out.println("1. 患者管理");
            System.out.println("2. 医生管理");
            System.out.println("3. 科室管理");
            System.out.println("4. 排班管理");
            System.out.println("5. 预约管理");
            System.out.println("6. 队列管理");
            System.out.println("7. 支付管理");
            System.out.println("8. 用户管理");
            System.out.println("9. 修改密码");
            System.out.println("0. 退出系统");

            System.out.print("请选择操作: ");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    if (AuthUtil.canAccessPatientFeatures(currentUser.getRole())) {
                        patientsManager.showPatientMenu();
                    } else {
                        System.out.println("权限不足！");
                    }
                    break;
                case "2":
                    if (AuthUtil.canAccessDoctorFeatures(currentUser.getRole())) {
                        doctorsManager.showDoctorMenu();
                    } else {
                        System.out.println("权限不足！");
                    }
                    break;
                case "3":
                    if (AuthUtil.canAccessAdminFeatures(currentUser.getRole())) {
                        departmentsManager.showDepartmentMenu();
                    } else {
                        System.out.println("权限不足！");
                    }
                    break;
                case "4":
                    if (AuthUtil.canAccessDoctorFeatures(currentUser.getRole())) {
                        schedulesManager.showScheduleMenu();
                    } else {
                        System.out.println("权限不足！");
                    }
                    break;
                case "5":
                    appointmentsManager.showAppointmentMenu();
                    break;
                case "6":
                    if (AuthUtil.canAccessDoctorFeatures(currentUser.getRole())) {
                        queuesManager.showQueueMenu();
                    } else {
                        System.out.println("权限不足！");
                    }
                    break;
                case "7":
                    paymentManager.showPaymentMenu();
                    break;
                case "8":
                    if (AuthUtil.canAccessAdminFeatures(currentUser.getRole())) {
                        usersManager.showUserMenu();
                    } else {
                        System.out.println("权限不足！");
                    }
                    break;
                case "9":
                    usersManager.changePassword();
                    break;
                case "0":
                    System.out.println("感谢使用医院门诊管理系统，再见！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    public static void main(String[] args) {
        CMSManager manager = new CMSManager();
        manager.start();

    }


}




